package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("ShoppingCartController")
@RequestMapping("/user/shoppingCart")
@Api("c端-购物车")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        //调用业务层，处理数据
        log.info("添加购物车:{}",shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        //返回结果给前端
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> selectShoppingCart(){
        //查询全部数据
        List<ShoppingCart> shoppingCartList = shoppingCartService.selectShoppingCart();
        return Result.success(shoppingCartList);
    }


    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result deleteShoppingCart(){
        //向业务层传递请求
        shoppingCartService.deleteShoppingCart();
        return Result.success();
    }


    /**
     * 删除单个id
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    public Result deletesingle(@RequestBody ShoppingCartDTO shoppingCartDTO){
        shoppingCartService.deletesingle(shoppingCartDTO);
        //删除成功返回
        return Result.success();
    }

}
